package com.ffapp.helper.util;

public class SubstringUtil {

    public static String subString(String allString,String key){
        String result="";
        //定时采集间隔
        int iIndex = allString.indexOf(key);
        if (iIndex != -1) {
            try {
//                int startIndex = allString.indexOf(":", iIndex);
//                int endIndex = allString.indexOf("\r\n", iIndex);
//                result = allString.substring(startIndex+1, endIndex);
                int startIndex = allString.indexOf(":", iIndex);
                int endIndex = allString.indexOf("\r", iIndex);
                result = allString.substring(startIndex+1, endIndex);
                if(result.contains("\n")){
                    result = result.substring(0,result.indexOf("\n"));
                }
            } catch (StringIndexOutOfBoundsException e) {
                return result ="数据异常，请重新获取";
//                return result ="";
            }
        }
        return result;
    }

    public static String getStringBetweenKey(String allString,String key1,String key2){
        String result ="";
        //定时采集间隔
        int iIndex = allString.indexOf(key1);
        if (iIndex != -1) {
            try {
                int startIndex = allString.indexOf(key1);
                int endIndex = allString.indexOf(key2);
                result = allString.substring(startIndex+1, endIndex-1);
            } catch (StringIndexOutOfBoundsException e) {
                return result ="数据异常，请重新获取";
            }
        }
        return result;
    }

    public static String getStringBeforeKey(String allString,String key){
        String result ="";
        //定时采集间隔
        int iIndex = allString.indexOf(key);
        if (iIndex != -1) {
            try {
                result = allString.substring(0, iIndex);
            } catch (StringIndexOutOfBoundsException e) {
                return result ="数据异常，请重新获取";
            }
        }
        return result;
    }

    public static String getStringAfterKey(String allString,String key){
        String result ="";
        //定时采集间隔
        int iIndex = allString.indexOf(key);
        if (iIndex != -1) {
            try {
                result = allString.substring(iIndex+1, allString.length());
            } catch (StringIndexOutOfBoundsException e) {
                return result ="数据异常，请重新获取";
            }
        }
        return result;
    }
}
